﻿using Demo.Net.API.Model.MongoModel;
using Demo.Net.MongoDB;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Validations;
using MongoDB.Bson;
using MongoDB.Driver;
using Newtonsoft.Json;
using System;

namespace Demo.Net.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MongoDBController : ControllerBase
    {

        //参见 AlwayMethod.txt
        private readonly IMongoDatabase _mongoDatabase;

        private readonly IMongoDatabase _testdatabase;

        private readonly IMongoDatabase _mgtdatabase;

        public MongoDBController(IMongoDatabase mongoDatabase, IMongoClientFactory mogoClientFactory) 
        {
            _mongoDatabase = mongoDatabase;
            //名称需要和program注入的一致
            _mgtdatabase = mogoClientFactory.Get("mgtest");
            _testdatabase = mogoClientFactory.Get("test");

        }




        [HttpGet]
        public async Task<IActionResult> MongoDBTest2()
        {
            TestMongoModel mogoobj = new TestMongoModel();
            mogoobj.name = "张三";
            mogoobj.age = 10;
            mogoobj.email = "666@qq.com";
            
            //3个库操作
            var collection=  _mongoDatabase.GetCollection<TestMongoModel>("test");
            await collection.InsertOneAsync(mogoobj);

            //var testcoll = _testdatabase.GetCollection<TestMongoModel>("test");
            //await testcoll.InsertOneAsync(mogoobj);


            //var mgtcoll = _mgtdatabase.GetCollection<TestMongoModel>("test");
            //await mgtcoll.InsertOneAsync(mogoobj);
            return Ok();
        }

        [HttpGet]
        public async Task<IActionResult> MongoDBTest3()
        {

            var collectionOptions = new CreateCollectionOptions();
            //创建一个集合
            await _mongoDatabase.CreateCollectionAsync("testname", collectionOptions);
            //删除集合
            await _mongoDatabase.DropCollectionAsync("testname");
            return Ok();
        }

        [HttpGet]
        public async Task<IActionResult> MongoDBTest4()
        {

            List<string> aaa = new List<string>() { "1","2","3" };
            //获取集合
            var collection = _mongoDatabase.GetCollection<TestMongoModel>("test");
            //批量插入
            //await   collection.InsertManyAsync(aaa);

            //查找
            var getresult =  collection.Find(p => p.name == "张三").FirstOrDefault();  //ToList

            //更新
            var filter = Builders<TestMongoModel>.Filter.Eq(p=>p.name, "张三");
            var update = Builders<TestMongoModel>.Update.Set(p=>p.age,20);
            var updresult = await collection.UpdateOneAsync(filter,update);

            //查找并更新
            var fupdresult = collection.FindOneAndUpdate(p => p.name == "张三", Builders<TestMongoModel>.Update.Set(x=>x.email, "ggg.@qq.com").Set(x=>x.age,60));

            //删除
            //   var delresult = await collection.DeleteOneAsync(p=>p.name=="张三");

            //var replacement = new TestMongoModel
            //{
            //     name ="lisi",
            //    age=30,
            //   email="642525840@qq.com"
            //};

            //var f2 = Builders<TestMongoModel>.Filter.Eq(x => x.name, "张三");

            //var rpcresult = await collection.ReplaceOneAsync(f2, replacement);


            return Ok();

        }
    }
}
